local skynet = require "skynet"
local cluster = require "skynet.cluster"
local filelog = require "filelog"
local timetool = require "timetool"
local timer = require "timer"
local redisdao = require "redisdao"
local filename = "autoreplay.lua"

local json = require "cjson"
json.encode_sparse_array(true,1,1)

require "imenum"

local autoreplay = {
	CMD = {},
	TIMER = {},
}

local session_data = {
	
}

function autoreplay.timer(msgname, timerid, noticemsg)
	local f = autoreplay.TIMER[msgname]
	if f and type(f) == "function" then
		f(timerid, noticemsg)
	end
end

function autoreplay.savewords(gm_rid, type_key, contents)
	--filelog.sys_error("--------autoreplay.savewords-------", gm_rid, type_key, contents)
	local key = "gm_savewords:" .. tostring(gm_rid)
	local status, result = redisdao.query_data(".gmmqredis", "hset", key, type_key, tostring(contents))
	--filelog.sys_error("--------autoreplay.query_data------", status, result)
end

function autoreplay.CMD.setwords(rid, setwords)
	filelog.sys_error("-----autoreplay.CMD.setwords------", rid, setwords)
	if not rid or not setwords then return end
	if rid == 0 or next(setwords) == nil then return end
	for key, message in pairs(setwords) do
		autoreplay.savewords(rid, tostring(key), message)
	end
end

function autoreplay.CMD.getword(rid, type_key)
	if not rid or not type_key then return end
	if rid == 0 or type_key == "" then return end
	---filelog.sys_error("------autoreplay.CMD.getword-------", rid, type_key)
	local key = "gm_savewords:" .. tostring(rid)
	local status, result = redisdao.query_data(".gmmqredis", "hget", key, type_key)
	---filelog.sys_error("-----autoreplay.cmd.getword, status, result------", status, result)
	if not status  then
		return nil
	else
		return result
	end
end

function autoreplay.CMD.getallsetwords(rid)
	if not rid then return end
	local key = "gm_savewords:" .. tostring(rid)
	local status, result = redisdao.query_data(".gmmqredis", "hkeys", key)
	filelog.sys_error("---status, result-----", status, result)
	if status == true then
		local allsetwords = {}
		for index, hkey in pairs(result) do
			local get_state, get_result = redisdao.query_data(".gmmqredis", "hget", key, hkey)
			if get_state == true then
				allsetwords[hkey] = get_result or "" 
			end
		end
		return json.encode(allsetwords)
	else
		return nil
	end
end

function autoreplay.CMD.sendautoreplay(type_key, from_rid, to_rid)
	if not type_key or not from_rid or not to_rid then return end
	---filelog.sys_error("--------autoreplay.CMD.sendautoreplay----", type_key, from_rid, to_rid)
	local from_state, is_exit, from_playerinfo = skynet.pcall(skynet.call, ".talkcache", "lua", "checkrid", from_rid)
	local to_state,to_is_exit, to_playerinfo = skynet.pcall(skynet.call, ".talkcache", "lua", "checkrid", to_rid)
	if not from_state or not to_state then return end
	if not is_exit or not to_is_exit then 
		filelog.sys_error("---------sendautoreplay------- is not exit----", is_exit, to_is_exit)
		return
	end
	local getword = autoreplay.CMD.getword(from_rid, type_key)
	if getword == nil or getword == "" then return end
	--[[
		message 是之前设置的内容，
		fromRid 是gm的id，
		toRid是玩家的，
		mType 填1，
		avtar是gm的头像，
		name是gm的名字
	--]]
	local allwords = {
		message = getword,
		fromRid = from_rid,
		toRid = to_rid, 
		mType = IMMTYPE.IMMTYPE_CHAT,
		avatar = tostring(from_playerinfo.logo),
		name = from_playerinfo.rolename,
	}
	local uuid = skynet.call(".nodeuuid", "lua", "getuuid")
    local newmessage = {
        keyid               = uuid,
        since_rid           = from_rid,
        send_to_rid         = to_rid,
        send_to_name        = "",
        msg                 = json.encode(allwords),
        sendtime            = timetool.get_time(),
    }

	local status, send_map = skynet.pcall(skynet.call, ".router", "lua", "get_role_data", to_rid)
	if not status or not send_map then
		skynet.send(".noticemgr", "lua", "push_notice", to_rid, 1)
        local status, success = skynet.pcall(skynet.call, ".talkcache", "lua", "pushmessageincache", from_rid, to_rid, newmessage, true)
        status, success = skynet.pcall(skynet.call, ".talkcache", "lua", "pushmessageincache", to_rid, from_rid, newmessage, false)
	else
		local send_to_talk_service = send_map.talk_service
        local status, success = skynet.pcall(skynet.call, send_to_talk_service, "lua", "cmd", "push_message", to_rid, newmessage, from_rid)
        local from_status, from_map = skynet.pcall(skynet.call, ".router", "lua", "get_role_data", from_rid)
        ---filelog.sys_error("------from_status, from_map----", from_status, from_map)
        if from_status == true and from_map ~= nil and from_map.talk_service ~= nil then
        	local from_to_talk_service = from_map.talk_service
        	local status, success = skynet.pcall(skynet.call, from_to_talk_service, "lua", "cmd", "push_message", from_rid, newmessage, to_rid)
        else
        	---filelog.sys_error("--------from_map------", from_map, from_rid)
        	skynet.send(".noticemgr", "lua", "push_notice", from_rid, 1)
        end
        ---放入缓存
        status, success = skynet.pcall(skynet.call, ".talkcache", "lua", "pushmessageincache", from_rid, to_rid, newmessage, true)
        status, success = skynet.pcall(skynet.call, ".talkcache", "lua", "pushmessageincache", to_rid, from_rid, newmessage, true)
	end
end

function autoreplay.CMD.sendvipchargeautoreplay(message, from_rid, to_rid, mType)
	if not message or not from_rid or not to_rid then return end
	filelog.sys_error("--------autoreplay.CMD.sendvipchargeautoreplay----", message, from_rid, to_rid)
	local from_state, is_exit, from_playerinfo = skynet.pcall(skynet.call, ".talkcache", "lua", "checkrid", from_rid)
	local to_state,to_is_exit, to_playerinfo = skynet.pcall(skynet.call, ".talkcache", "lua", "checkrid", to_rid)
	if not from_state or not to_state then return end
	if not is_exit or not to_is_exit then 
		filelog.sys_error("---------sendvipchargeautoreplay------- is not exit----", is_exit, to_is_exit)
		return
	end
	if message == nil or message == "" then return end
	--[[
		message 是之前设置的内容，
		fromRid 是gm的id，
		toRid是玩家的，
		mType 填1，
		avtar是gm的头像，
		name是gm的名字
	--]]
	local allwords = {
		message = message,
		fromRid = from_rid,
		toRid = to_rid, 
		mType = mType,
		avatar = tostring(from_playerinfo.logo),
		name = from_playerinfo.rolename,
	}
	local uuid = skynet.call(".nodeuuid", "lua", "getuuid")
    local newmessage = {
        keyid               = uuid,
        since_rid           = from_rid,
        send_to_rid         = to_rid,
        send_to_name        = "",
        msg                 = json.encode(allwords),
        sendtime            = timetool.get_time(),
    }

	local status, send_map = skynet.pcall(skynet.call, ".router", "lua", "get_role_data", to_rid)
	if not status or not send_map then
		skynet.send(".noticemgr", "lua", "push_notice", to_rid, 1)
        local status, success = skynet.pcall(skynet.call, ".talkcache", "lua", "pushmessageincache", from_rid, to_rid, newmessage, true)
        status, success = skynet.pcall(skynet.call, ".talkcache", "lua", "pushmessageincache", to_rid, from_rid, newmessage, false)
	else
		local send_to_talk_service = send_map.talk_service
        local status, success = skynet.pcall(skynet.call, send_to_talk_service, "lua", "cmd", "push_message", to_rid, newmessage, from_rid)
        local from_status, from_map = skynet.pcall(skynet.call, ".router", "lua", "get_role_data", from_rid)
        ---filelog.sys_error("------from_status, from_map----", from_status, from_map)
        if from_status == true and from_map ~= nil and from_map.talk_service ~= nil then
        	local from_to_talk_service = from_map.talk_service
        	local status, success = skynet.pcall(skynet.call, from_to_talk_service, "lua", "cmd", "push_message", from_rid, newmessage, to_rid)
        else
        	---filelog.sys_error("--------from_map------", from_map, from_rid)
        	skynet.send(".noticemgr", "lua", "push_notice", from_rid, 1)
        end
        ---放入缓存
        status, success = skynet.pcall(skynet.call, ".talkcache", "lua", "pushmessageincache", from_rid, to_rid, newmessage, true)
        status, success = skynet.pcall(skynet.call, ".talkcache", "lua", "pushmessageincache", to_rid, from_rid, newmessage, true)
	end
end


skynet.start(function()
	skynet.dispatch("lua",function(session, source, cmd, ...)
		if cmd == "timer" then
			local f = autoreplay.timer
			if f then
				skynet.retpack(f(...))
			else
				skynet.retpack()
			end
		else
        	local f = autoreplay.CMD[cmd]
        	if f then
            	skynet.retpack(f(...))
        	else
            	skynet.retpack()
        	end
        end
    end) 
end)